********************************************************************************
*                                                                              *
*                   CREATE ADJUSTED RTI VALUES BASED ON LFS                    *
*                                                                              *
********************************************************************************

global startyear = 2008 // 1 year before the start of the study period (because LFSyeardiff <=1 )
global endyear = 2020

	use "H:\Robots and Workers\Data\LFSX_before.dta", clear
	keep rinpersoon lfsyear lfsquarter isco08 educ_soi3 educ_soi5

	merge 1:1 rinpersoon lfsyear using "H:\Robots and Workers\Data\LFSX_extnew.dta", keep(1 2 3 4 5) update keepusing(educ_soi5 educ_soi3 isco08_extnew) nogen
	replace isco08 = isco08_extnew if isco08==.
	
	rename isco08 ISCO08Code
	forvalues year = $startyear(1)$endyear {
		if `year' <= 2018 {
			merge m:1 ISCO08Code lfsyear using "H:\Robots and Workers\Base files\RTI\onet-ISCO08RTI_LFSX_`year'.dta", nogen keep(1 3 4 5) update keepusing(RTI* nr_cog_anal nr_cog_pers r_cog r_man nr_man_phys nr_man_pers nr_man_persInt)
		}
		if `year' > 2018 {
			merge m:1 ISCO08Code lfsyear using "H:\Robots and Workers\Base files\RTI\onet-ISCO08RTI_LFSX_copy2018_`year'.dta", nogen keep(1 3 4 5) update keepusing(RTI* nr_cog_anal nr_cog_pers r_cog r_man nr_man_phys nr_man_pers nr_man_persInt)
		}
	}
	rename (RTI RTIw) (rti rtiw)	
	rename ISCO08Code isco08
	foreach var in rtiw nr_cog_anal nr_cog_pers r_cog r_man nr_man_phys nr_man_pers nr_man_persInt {
		capture drop `var'
	}
	su rti
	replace rti = (rti-r(mean))/r(sd)
	rename rti rti_ext
	collapse (mean) rti_ext, by(isco08)
	qui do "H:\Robots and Workers\Do-files\Variable labels.do"
	compress
	drop if rti_ext == . | isco08 == .
	save "H:\Robots and Workers\Base files\RTI\RTI_populationnormalised.dta", replace